package com.admin.education.common.utils;

import com.admin.education.common.exceptions.ResourceNotFoundException;
import com.admin.education.common.response.IResponseMessage;
import com.admin.education.common.exceptions.BadRequestException;
import com.admin.education.common.exceptions.UnauthorizedException;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.models.User;
import com.admin.education.services.Interfaces.IUserService;
import org.springframework.context.ApplicationContext;

import javax.servlet.http.HttpServletRequest;

public class OpenIdUtil {
    public static User getUser(HttpServletRequest request) throws UnauthorizedException, BadRequestException, ResourceNotFoundException {
        final String openIdHeader = request.getHeader("OpenId");
        IResponseMessage responseMessage = null;
        String accept = request.getHeader("Accept");
        if (openIdHeader == null) {
            throw new UnauthorizedException("OpenId为空");
        }
        ApplicationContext applicationContext= InjectUtil.getApplicationContext();

        IUserService userService= applicationContext.getBean(IUserService.class);
         ServicesExcuteResult<User> result = userService.GetUser(openIdHeader);
         if(result.isSuccess()){
            return result.getData();
         }
         throw new BadRequestException(result.getErrorMessage());
    }
}
